package com.lsh.oms.core.proxy.service;

import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSON;
import com.lsh.base.common.exception.BusinessException;
import com.lsh.base.common.model.CommonResult;
import com.lsh.ofc.api.dto.ReturnDetailDTO;
import com.lsh.ofc.api.dto.ReturnHeadDTO;
import com.lsh.ofc.api.dto.SoHeadDTO;
import com.lsh.ofc.api.service.OrderService;
import com.lsh.ofc.api.service.ReturnRpcService;
import com.lsh.ofc.api.service.SoService;
import com.lsh.oms.core.model.order.OrderRoDetail;
import com.lsh.oms.core.model.order.OrderRoHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component
public class OfcProxy {

    private static Logger logger = LoggerFactory.getLogger(OfcProxy.class);

    @Autowired
    private OrderService ofcOrderService;

    @Autowired
    private SoService soService;

    @Autowired
    private ReturnRpcService returnRpcService;


    /**
     * @param orderCode
     * @return
     */
    public List<SoHeadDTO> getSoInfo(Long orderCode){

        List<SoHeadDTO> soHeadDTOList = soService.querySignOrders(orderCode);

        return soHeadDTOList;
    }
    /**
     * @param orderRoHead
     * @return
     */
    public CommonResult<List<String>> createRo(OrderRoHead orderRoHead) {

        ReturnHeadDTO returnHead = new ReturnHeadDTO();

        returnHead.setReturnCode(orderRoHead.getReturnOrderId());
        returnHead.setOrderCode(orderRoHead.getOrderId());
        returnHead.setRegionCode(orderRoHead.getZoneId());
        returnHead.setAddressCode(orderRoHead.getAddressId());
        returnHead.setWarehouseCode(orderRoHead.getWarehouseId());
        returnHead.setWarehouseName(orderRoHead.getWarehouseName());
        returnHead.setCreateTime(orderRoHead.getCreatedAt());
        returnHead.setOrderAmount(orderRoHead.getMoney());
        returnHead.setCount(1);//订单数量 TODO 需要计算
        List<ReturnDetailDTO> returnDetailDTOList = new ArrayList<>();
        ReturnDetailDTO returnDetail;
        for (OrderRoDetail orderRoDetail : orderRoHead.getDetails()) {
            returnDetail = new ReturnDetailDTO();
            returnDetail.setDetailId(orderRoDetail.getId());
            returnDetail.setGoodsCode(orderRoDetail.getSkuId());
            returnDetail.setGoodsName(orderRoDetail.getSkuName());
            returnDetail.setGoodsSaleUnit(orderRoDetail.getSaleUnit());
            returnDetail.setGoodsPrice(orderRoDetail.getPrice());
            returnDetail.setGoodsQty(orderRoDetail.getQty());
            returnDetail.setGoodsAmount(orderRoDetail.getMoney());
            returnDetail.setSkuCode(orderRoDetail.getItemId());
            returnDetail.setSkuQty(orderRoDetail.getRealQty());

            returnDetailDTOList.add(returnDetail);
        }

        returnHead.setDetails(returnDetailDTOList);

        return returnRpcService.createRo(returnHead);
    }


    /**
     * @param returnCode
     * @return
     */
    public CommonResult<Object> queryRoStatus(Long returnCode) {

        return returnRpcService.queryRoStatus(returnCode);
    }

    /**
     *
     * @param orderCode
     * @param logSign
     * @return
     */
    public CommonResult<Boolean> cancelOrder(Long orderCode, String logSign) {
        String message = "[OfcProxy][cancelOrder][" + logSign + "]";

        CommonResult<Boolean> commonResult;
        try {
            logger.info(message + "[start][Request params:" + orderCode + "]");
            commonResult = ofcOrderService.cancelOrder(orderCode);
            logger.info(message + "[end][Response result:" + JSON.toJSONString(commonResult) + "]");
        } catch (BusinessException e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            return this.buildResult(false, null);
        } catch (RpcException rpcEx) {
            logger.error(message + "[error][Error message:" + rpcEx.getMessage() + "]");
            logger.error(rpcEx.getMessage(), rpcEx);
            return this.buildResult(false, "操作超时,请稍后重试!");
        }

        return this.buildResultForReturn(commonResult, message);
    }

    /**
     *
     * @param orderCode
     * @param logSign
     * @return
     */
    public CommonResult<Boolean> cancelOrderNotify(Long orderCode, String logSign) {
        String message = "[OfcProxy][cancelOrderNotify][" + logSign + "]";

        CommonResult<Boolean> commonResult;
        try {
            logger.info(message + "[start][Request params:" + orderCode + "]");
            commonResult = ofcOrderService.cancelOrderNotify(orderCode);
            logger.info(message + "[end][Response result:" + JSON.toJSONString(commonResult) + "]");
        } catch (BusinessException e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            return this.buildResult(false, null);
        } catch (RpcException rpcEx) {
            logger.error(message + "[error][Error message:" + rpcEx.getMessage() + "]");
            logger.error(rpcEx.getMessage(), rpcEx);
            return this.buildResult(false, "操作超时,请稍后重试!");
        }

        return this.buildResultForReturn(commonResult, message);
    }

    public boolean isWumartOfc(String logSign) {
        String message = "[OfcProxy][" + logSign + "]";
        boolean flag = false;
        try {
            flag = ofcOrderService.isWumartOfc();
        } catch (Exception e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
        }
        return flag;
    }

    /**
     *
     * @param orderCode
     * @param logSign
     * @return
     */
    public CommonResult<Boolean> isForceCancel(Long orderCode, String logSign) {
        String message = "[OfcProxy][isForceCancel][" + logSign + "]";

        boolean isForceCancel;
        try {
            logger.info(message + "[start][Request params:" + orderCode + "]");
            isForceCancel = ofcOrderService.isForceCancel(orderCode);
            logger.info(message + "[end][Response result:" + Boolean.valueOf(isForceCancel).toString() + "]");
        } catch (BusinessException e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            return this.buildResult(false, null);
        } catch (RpcException rpcEx) {
            logger.error(message + "[error][Error message:" + rpcEx.getMessage() + "]");
            logger.error(rpcEx.getMessage(), rpcEx);
            return this.buildResult(false, "操作超时,请稍后重试!");
        }

        return this.buildResult(isForceCancel, null);
    }

    /**
     *
     * @param commonResult
     * @param message
     * @return
     */
    private CommonResult<Boolean> buildResultForReturn(CommonResult<Boolean> commonResult, String message) {
        if (commonResult.getCode().equals(CommonResult.SUCCESS)) {
            Boolean flag = commonResult.getData();
            if (flag != null && flag) {
                return this.buildResult(true, null);
            } else {
                logger.error(message + "[error][Error message:" + commonResult.getMessage() + "]");
                return this.buildResult(false, null);
            }
        }

        return this.buildResult(false, null);
    }

    /**
     *
     * @param isSuccess
     * @param message
     * @return
     */
    private CommonResult<Boolean> buildResult(boolean isSuccess, String message) {
        CommonResult<Boolean> result = new CommonResult<>();
        result.setData(isSuccess);
        result.setMessage(message);
        return result;
    }

}
